<template>
	<div class="dragVerify">
		<div class="spout" ref="spout">
			<div class="slidingBlock"
				ref="slidingBlock"
				:style="{left:`${place}%`}"
				@mousedown="mousedown($event)"
				@touchstart="mousedown($event)"
				:class="place < distance?'unfinished':'complete'"
				>
			</div>
			<div class="succeedBox" :class="place >= distance?'succeedText':''" :style="{width: `${place}%`}"></div>
		</div>
	</div>
</template>

<script>
	export default{
		name: 'dragVerify',
		data(){
			return{
				place: 0,
				sliding:{
					isDown: true,
					X: 0 // 初始X值
				},
				move: {
					X: 0 // 移动X值
				},
				spoutW: 0,
				slidingBlockW: 0,
				distance: 1 , // 要移动的距离
			}
		},
		mounted() {
			
		},
		methods:{
			mousedown(e){
				if(this.place < this.distance){
					this.sliding.isDown = true
					// 计算百分比
					this.spoutW = this.$refs.spout.offsetWidth
					this.slidingBlockW = this.$refs.slidingBlock.offsetWidth
					this.distance = 100-this.slidingBlockW/this.spoutW*100
				}
				this.sliding.X = e.x
				this.$refs.spout.addEventListener('mousemove', (e) => {
					if(this.sliding.isDown){
						this.move.X = e.x
						if(this.place >= this.distance){
							this.place = this.distance
						} else if(this.place >= 0 && this.place < this.distance){
							this.place = (this.move.X -this.sliding.X)/this.spoutW*100
						}
						if(this.place <= 0){
							this.place = 0
							return
						}
					}
					e.preventDefault();
				})
				this.$refs.spout.addEventListener('touchmove', (e) => {
					if(this.sliding.isDown){
						this.move.X = e.x
						if(this.place >= this.distance){
							this.place = this.distance
						} else if(this.place >= 0 && this.place < this.distance){
							this.place = (this.move.X -this.sliding.X)/this.spoutW*100
						}
						if(this.place <= 0){
							this.place = 0
							return
						}
					}
					e.preventDefault();
				})
				document.onmouseup = (e) => {
					if(this.place == this.distance){
						this.$emit('setVerify',true)
					} else {
						this.sliding.isDown = false
						this.place = 0
						this.$emit('setVerify',false)
					}
				}
				document.ontouchend = (e) => {
					if(this.place == this.distance){
						this.$emit('setVerify',true)
					} else {
						this.sliding.isDown = false
						this.place = 0
						this.$emit('setVerify',false)
					}
				}
			},
		}
	}
</script>

<style scoped="scoped">
	.dragVerify{
		border: 1px solid #E1E1E1;
		height: 40px;
		background: #EEEEEE;
	}
	.spout{
		line-height: 40px;
		height: 40px;
		/* text-align: center; */
		position: relative;
		width: 293px;
	}
	.spout::before{
		content: '请按住滑块，拖动到最右边';
		width: 233px;
		top: 0;
		right: 0;
		bottom: 0;
		color: #919593;
		font-size: 16px;
		text-align: center;
		position: absolute;
	}
	.succeedText::before{
		content: '验证通过';
		width: 233px;
		top: 0;
		right: 0;
		bottom: 0;
		color: #FFFFFF;
		font-size: 16px;
		text-align: center;
		position: absolute;
	}
	.succeedBox{
		color: #FFFFFF;
		font-size: 16px;
		text-align: center;
		line-height: 38px;
		height: 38px;
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #23B14D;
		z-index: 8;
	}
	.slidingBlock{
		width: 60px;
		/* height: 38px; */
		height: calc(100% - 0.1rem);
		border: 1px solid #E1E1E1;
		border-top: none;
		/* border-bottom: none; */
		border-left: none;
		background: #FFFFFF;
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		/* margin-left: -1px; */
		cursor: move;
		z-index: 9;
	}
	.slidingBlock::after{
		content: '';
		position: absolute;
		background-size: 100% 100%;
		background-repeat: no-repeat;
		width: 20px;
		height: 20px;
		left: 50%;
		top: 50%;
		margin-left: -10px;
		margin-top: -10px;
	}
	.unfinished::after{
		background-image: url(../../../assets/image/login/dragJt.png);
	}
	.complete::after{
		background-image: url(../../../assets/image/login/dragCg.png);
	}
</style>
